home *** CD-ROM | disk | FTP | other *** search
/ BBS in a Box 3 / BBS in a box - Trilogy III.iso / Files / Prog / H-K / KeMo Lib 1.5 / KeMoTest.c < prev   
Encoding:
C/C++ Source or Header  |  1993-09-30  |  8.4 KB  |  318 lines  |  [TEXT/KAHL]

  1. /*
  2.  *  KeMoTest.c
  3.  *
  4.  *  Copyright (c) 1992,1993 Dan Costin.  All rights reserved.
  5.  *
  6.  */
  7.  
  8. #include <stdio.h>
  9. #include <stdlib.h>
  10. #include <ctype.h>
  11. #include <time.h>
  12. #include "KeMo.h"
  13.  
  14. void AccuracyTest(void);
  15. void ResponseTest(void);
  16. void TimerTest(void);
  17. void ScreenTest(void);
  18. void KeyCodesTest(void);
  19. void ShowHideMBar(void); 
  20.  
  21. /* Main menu */
  22. main()
  23. {
  24.     char ans[20];
  25.     long err;
  26.  
  27.     printf("Welcome to KeMo's test suite!\n");
  28.  
  29.     KeMoInit(KeMoQuiet + KeMoAltKeys);    /* no beeps on errors (but do show alerts) */
  30.                                         /* also, set keyboard to differentiate between
  31.                                             left and right modifier keys */
  32.     
  33.     while(1) {
  34.         printf("\nThe following tests are available:\n");
  35.         printf("    [A]ccuracy        - measures keyboard and mouse detection accuracy\n");
  36.         printf("                        (uses KeMoAccuracy)\n");
  37.         printf("    [E]nd Programs    - end all other running programs (uses KeMoQuitAllApps)\n");
  38.         printf("    [K]eys            - reports symbol for keys pressed (uses KeMoCode2Asc)\n");
  39.         printf("    [M]enus           - show/hide menu bar\n");
  40.         printf("    [R]esponses       - checks detection of up and down calls to KeMoWait\n");
  41.         printf("    [S]creen Refresh  - calls KeMoSynch on every screen refresh for 20 seconds\n");
  42.         printf("                        and reports what should be the monitor's refresh rate\n");
  43.         printf("    [T]imer           - checks the accuracy of the timer against the Mac clock\n");
  44.         printf("    [Q]uit\n");
  45.         printf("\nPlease select one of the above: ");
  46.         fflush(stdout);
  47.         fflush(stdin);
  48.         
  49.         fgets(ans, 10, stdin);
  50.         
  51.         switch (toupper(*ans)) {
  52.             case 'A': AccuracyTest(); break;
  53.             case 'E': KeMoQuitAllApps(); break;
  54.             case 'K': KeyCodesTest(); break;
  55.             case 'M': ShowHideMBar(); break;
  56.             case 'R': ResponseTest(); break;
  57.             case 'T': TimerTest(); break;
  58.             case 'S': ScreenTest(); break;
  59.             case 'Q': ExitToShell(); break;
  60.             default: printf("Your choice is not recognized.  Please try again.\n");
  61.                     continue;
  62.             }
  63.         }
  64. }
  65.  
  66.  
  67. void ShowHideMBar() 
  68. {
  69.     long err;
  70.     
  71.     if ( (err = KeMoShowMBar()) && (err = KeMoHideMBar()) )
  72.         printf("\n*** Call to menu bar operation returned error code %ld.\n", err);
  73.     return;
  74. }
  75.  
  76. void KeyCodesTest() {
  77.     KeMoParms parms;
  78.     char c;
  79.     long err;
  80.  
  81.     if (err = KeMoSelect(KeMoOneKey))    {    /* select one keyboard */
  82.         printf("\n*** Couldn't select keyboard only at this time (%ld).\n", err);
  83.         return;
  84.         }
  85.  
  86.     c = 0;
  87.     printf("\nPress one key at a time.  To end, press 'q' or press no keys for 5 seconds.\n");
  88.  
  89.     while (c != 'q' && c != '!') {    /* end with q or timeout (shows up as ! returned
  90.                                         by KeMoCode2Asc) */
  91.                                         
  92.         KeMoWait(KeMoDown, 5000L, &parms);    /* wait for a kepypress up to 5 seconds */
  93.         
  94.         c = KeMoCode2Asc(parms.key);    /* convert the key code to ASCII representation */
  95.         
  96.         if (c < 10)                    /* keypad numbers */
  97.             printf("K%c ", c+'0');
  98.         else if (c < 20)            /* function keys 1-9 */
  99.             printf("F%c ", c-10+'0');
  100.         else if (c < 30)            /* function keys 10-15 */
  101.             printf("F1%c ", c-20+'0');
  102.         else                        /* other key */
  103.             printf("%c ", c);
  104.         fflush(stdout);
  105.         }
  106.     printf("\n\n");
  107.     KeMoReset();        /* undo selection of keyboard only */
  108. }
  109.  
  110. void ScreenTest()
  111. {
  112.     long x;
  113.     long t, i;
  114.  
  115.     if (x = KeMoSync(1)) {
  116.         printf("Can't do screen sync: error %ld\n", x);
  117.         return;
  118.         }
  119.         
  120.     printf("\nThis test takes 20 seconds.  Please be patient.\n");
  121.     
  122.     i = 0;                /* Time is updated once a second by the Mac
  123.                             (it's a low-memory Mac variable) */
  124.     t = Time + 1;
  125.     while (t > Time);    /* wait for a fresh second */
  126.     
  127.     while (t + 20 > Time) {        /* call KeMoSync for exactly 20 seconds */
  128.         KeMoSync(1);
  129.         i++;
  130.         }
  131.         
  132.     printf("This monitor's screen refresh rate is approximately %.1f Hz.\n", i/20.0);
  133.     
  134.     return;
  135. }
  136.  
  137.  
  138. void TimerTest()
  139. {
  140.     long t;
  141.     
  142.     printf("\nThis test runs for 10 seconds.  Press any key in about 7 seconds (at beep).\n");
  143.     printf("If you hear a second beep, you did not press a key in time (press a key anyway\n");
  144.     printf("to stop the test).\n");
  145.     
  146.     t = KeMoTimerTest();        /* this function takes care of this test */
  147.                                 /* it returns microseconds */
  148.     if (t < 0)
  149.         printf("\nThere was an error: %ld.  Perhaps you didn't press the key early enough?\n", t);
  150.     else
  151.         printf("\nTimer accuracy while looking for keyboard: %ld msecs/sec.\n", (long)(t/1000.0 + .5));
  152.     
  153.     printf("\nRunning Delay test (1 second).\n");
  154.     KeMoTimerStart();
  155.     KeMoDelay(1000L);
  156.     t = KeMoTimerStop(KeMoNoCorrection);
  157.     
  158.     printf("A delay of 1000 msecs takes %ld msecs to complete.\n", t);
  159. }
  160.  
  161. void ResponseTest()
  162. {
  163.     long err, i, flags, done;
  164.     KeMoParms parms;
  165.     char ans[20];
  166.  
  167.     flags = 0; done = 0;
  168.     
  169.     while (!done) {
  170.         if (!flags)
  171.             printf("\nPlease select a device to be tested from the following list:\n\n");
  172.         else
  173.             printf("\nPlease select additional devices to be tested from the following list:\n\n");
  174.                         /* go through devices 2-4,8-15 */
  175.         for (i = 2; i < 16; i == 4 ? i = 8 : i++) {
  176.             if (KeMoDevArray[i] && !(flags & (1 << i))) {
  177.                 printf("\t%2ld: KeMoDev%1lX (%s)\n", i, i, 
  178.                 i == 2 && (KeMoDevType[i] == 2 || KeMoDevType[i] == 3) ? "keyboard" : (
  179.                 i == 2 || KeMoDevType[i] == 2 || KeMoDevType[i] == 3 ? "keyboard?" : "mouse/other"));
  180.                 }
  181.             }
  182.             
  183.         printf("\n\t 0) Cancel\n");
  184.         if (flags)
  185.             printf("\t D) Done - use devices selected so far\n");    
  186.         
  187.         printf("\nChoice: ");
  188.         fflush(stdout);
  189.         
  190.         fgets(ans, 10, stdin);
  191.         
  192.         if (*ans == 'd' || *ans == 'D') break;
  193.         
  194.         i = atoi(ans);
  195.         
  196.         if (i == 0) return;
  197.         
  198.         if (KeMoDevArray[i] && !(flags & (1 << i))) {
  199.             flags |= 1 << i;
  200.             continue;
  201.             }
  202.         else {
  203.             printf("\n*** INVALID SELECTION (%ld). ***\n", i);
  204.             continue;
  205.             }
  206.         }
  207.         
  208.     if (err = KeMoSelect(flags))        /* select the keyboard */
  209.         printf("\n*** Couldn't select desired device at this time (err = %ld).\n", err);
  210.     else {
  211.         printf("\nThis test calls KeMoWait repeatedly with a 2 second timeout.\n");
  212.         printf("Looking for 5 down events: "); fflush(stdout);
  213.         for (i = 0; i < 5; i++) {
  214.             KeMoWait(KeMoDown, 2000L, &parms);
  215.             printf("%c-%s ", KeMoCode2Asc(parms.key), (parms.updown == KeMoDown ? "down" : 
  216.                 (parms.updown == KeMoUp ? "up" : 
  217.                 (parms.updown == KeMoTimedOut ? "timeout" : "???"))));
  218.             fflush(stdout);
  219.             }
  220.         printf("\n");
  221.  
  222.         printf("Looking for 5 up events: "); fflush(stdout);
  223.         for (i = 0; i < 5; i++) {
  224.             KeMoWait(KeMoUp, 2000L, &parms);
  225.             printf("%c-%s ", KeMoCode2Asc(parms.key), (parms.updown == KeMoDown ? "down" : 
  226.                 (parms.updown == KeMoUp ? "up" : 
  227.                 (parms.updown == KeMoTimedOut ? "timeout" : "???"))));
  228.             fflush(stdout);
  229.             }
  230.         printf("\n");
  231.  
  232.         printf("Looking for 5 up or down events: "); fflush(stdout);
  233.         for (i = 0; i < 5; i++) {
  234.             KeMoWait(KeMoUpDown, 2000L, &parms);
  235.             printf("%c-%s ", KeMoCode2Asc(parms.key), (parms.updown == KeMoDown ? "down" : 
  236.                 (parms.updown == KeMoUp ? "up" : 
  237.                 (parms.updown == KeMoTimedOut ? "timeout" : "???"))));
  238.             fflush(stdout);
  239.             }
  240.         printf("\n");
  241.         }
  242.                 
  243.     KeMoReset();
  244.     return;
  245. }
  246.  
  247. void AccuracyTest()
  248. {
  249.     float t;
  250.     long err, i, flags, done;
  251.     KeMoParms parms;
  252.     char ans[20];
  253.     short x;
  254.  
  255.     flags = 0; done = 0;
  256.     
  257.     while (!done) {
  258.         if (!flags)
  259.             printf("\nPlease select a device to be tested from the following list:\n\n");
  260.         else
  261.             printf("\nPlease select additional devices to be tested from the following list:\n\n");
  262.                         /* go through devices 2-4,8-15 */
  263.         for (i = 2; i < 16; i == 4 ? i = 8 : i++) {
  264.             if (KeMoDevArray[i] && !(flags & (1 << i))) {
  265.                 printf("\t%2ld: KeMoDev%1lX (%s)\n", i, i, 
  266.                 i == 2 && (KeMoDevType[i] == 2 || KeMoDevType[i] == 3) ? "keyboard" : (
  267.                 i == 2 || KeMoDevType[i] == 2 || KeMoDevType[i] == 3 ? "keyboard?" : "mouse/other"));
  268.                 }
  269.             }
  270.             
  271.         printf("\n\t 0) Cancel\n");
  272.         if (flags)
  273.             printf("\t D) Done - use devices selected so far\n");    
  274.         
  275.         printf("\nChoice: ");
  276.         fflush(stdout);
  277.         
  278.         fgets(ans, 10, stdin);
  279.         
  280.         if (*ans == 'd' || *ans == 'D') break;
  281.         
  282.         i = atoi(ans);
  283.         
  284.         if (i == 0) return;
  285.         
  286.         if (KeMoDevArray[i] && !(flags & (1 << i))) {
  287.             flags |= 1 << i;
  288.             continue;
  289.             }
  290.         else {
  291.             printf("\n*** INVALID SELECTION (%ld). ***\n", i);
  292.             continue;
  293.             }
  294.         }        
  295.     
  296.     if (err = KeMoSelect(flags)) {
  297.         printf("\n*** Oops! That selection cannot be made at this time (%ld).\n", err);
  298.         return;
  299.         }
  300.     
  301.     Delay(60L, 0);
  302.     
  303.     printf("End the test by pressing the appropriate device(s).\n");
  304.     printf("The test will be automatically stopped after about 10 seconds.\n");
  305.     
  306.     t = KeMoAccuracy();    /* accuracy is returned in microseconds */
  307.     
  308.     KeMoReset();
  309.     
  310.     if (t < 0)
  311.         printf("\nThe desired accuracy test could not be performed: error %ld.\n", (long)t);
  312.     else
  313.         printf("\nThe timing accuracy is approximately +/- %0.1f msec.\n\n", 
  314.                     t/1000.0/2.0);
  315.  
  316.     return;
  317. }
  318.